package com.ybt.communicate.tcp.parser;

import java.io.FileInputStream;

import org.dom4j.Element;

import com.ybt.communicate.core.exception.ProtocolException;
import com.ybt.communicate.core.parser.IProtocolParser;
import com.ybt.communicate.core.parser.impl.CommonProtocolParser;
import com.ybt.communicate.tcp.configration.TCPBaseConfig;
import com.ybt.communicate.tcp.configration.TCPProtocolConfig;

public class TCPParser implements IProtocolParser<TCPProtocolConfig> {

    public void convertCommon(TCPProtocolConfig protocolConfig, Element protocol)
            throws ProtocolException {
        // TODO Auto-generated method stub
        TCPBaseConfig config = protocolConfig.getCommonConfig();
        Element common = protocol.addElement("common");
        if (config.getHost() != null) {
            common.addAttribute("host", config.getHost());
        }
        common.addAttribute("port", config.getPort() + "");
        if (config.getWorkingThread() != 0) {
            common
                    .addAttribute("workingThread", config.getWorkingThread()
                            + "");
        }

        if (config.getType() != null) {
            common.addAttribute("mode", config.getType() + "");
        }

        // 编码
        if (config.getEncode() != null) {
            common.addAttribute("encode", config.getEncode());
        }
        // 读写策略
        if (config.getStrategy() != null) {
            common.addAttribute("strategy", config.getStrategy());
        }

        // 读取数据后默认处理方式
        if (config.getAction() != null) {
            common.addAttribute("action", config.getAction());
        }

        // 读超时时间
        if (config.getReadTimeout() > 0) {
            common.addAttribute("readTimeout", config.getReadTimeout() + "");
        }
        // 连接超时时间
        if (config.getConnTimeout() > 0) {
            common.addAttribute("connTimeout", config.getConnTimeout() + "");
        }
    }

    public void convertRequest(TCPProtocolConfig protocolConfig,
            Element protocol) throws ProtocolException {
        // TODO Auto-generated method stub

    }

    public void convertResponse(TCPProtocolConfig protocolConfig,
            Element protocol) throws ProtocolException {
        // TODO Auto-generated method stub
    }

    public TCPProtocolConfig parseCommon(TCPProtocolConfig config,
            Element common) throws ProtocolException {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        if (config.getCommonConfig() == null) {
            config.setCommonConfig(new TCPBaseConfig());
        }
        TCPBaseConfig commonConfig = config.getCommonConfig();

        // host
        String host = common.attributeValue("host");
        if (host != null) {
            commonConfig.setHost(host);
        }

        // 工作线程数
        String workingThread = common.attributeValue("workingThread");
        if (workingThread != null) {
            commonConfig.setWorkingThread(Integer.valueOf(workingThread));
        }

        // 长、端连接
        String mode = common.attributeValue("mode");
        if (mode != null) {
            commonConfig.setType(mode);
        }

        // 端口
        String port = common.attributeValue("port");
        commonConfig.setPort(Integer.valueOf(port));

        // 写策略
        String strategy = common.attributeValue("strategy");
        if (strategy != null) {
            commonConfig.setStrategy(strategy);
        }

        // 请求数据的编码方式
        String encode = common.attributeValue("encode");
        if (encode != null) {
            commonConfig.setEncode(encode);
        }

        // 读取完数据后默认处理方式
        String action = common.attributeValue("action");
        if (action != null) {
            commonConfig.setAction(action);
        }

        // 连接超时时间
        String connTimeout = common.attributeValue("connTimeout");
        if (connTimeout != null) {
            commonConfig.setConnTimeout(Integer.valueOf(connTimeout));
        }

        // 读超时时间
        String readTimeout = common.attributeValue("readTimeout");
        if (readTimeout != null) {
            commonConfig.setReadTimeout(Integer.valueOf(readTimeout));
        }

        return config;
    }

    public TCPProtocolConfig parseRequest(TCPProtocolConfig config,
            Element request) throws ProtocolException {
        // TODO Auto-generated method stub
        return null;
    }

    public TCPProtocolConfig parseResponse(TCPProtocolConfig config,
            Element response) throws ProtocolException {
        // TODO Auto-generated method stub
        return null;
    }

    public static void main(String[] args) {
        String f = "E:\\eclipse\\workspace\\YBT_Simple\\conf\\protocols\\TcpService_8080.xml";
        CommonProtocolParser<TCPProtocolConfig> parser = new CommonProtocolParser<TCPProtocolConfig>();
        try {
            TCPProtocolConfig config = parser.parse(new FileInputStream(f));
            System.out.println(parser.convert(config));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
